<template>
  <div id="tiktok-cps">
    <c-title text="抖音精选"></c-title>
    <div class="top" v-if="show_flag">
      <van-search shape="round" disabled @click="link" placeholder="输入关键词/口令搜索" />
    </div>
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="item in CarouseList" :key="item.id">
        <img :src="item.pic" style="width: 100%;" @click="link(item.url)" />
      </van-swipe-item>
    </van-swipe>
    <div class="ment" v-if="(button_group&&button_group.length!==0)">
      <c-ment :datas="fun.chunk(button_group, 10)" @open_dialog="open_dialog"></c-ment>
    </div>
    <div class="content">
      <div class="nav">
        <div class="category" v-if="(category_list && category_list.length!==0)">
          <span v-for="(item, i) in category_list" @click="category_handle(item.cate_id, i)"
            :class="{ 'current_category': currrent_category_index == i }">{{ item.name }}</span>
        </div>
        <div class="tab"  v-if="show_flag">
          <span @click="choose(1)" :class="{ active: choose_index == 1 }">综合</span>
          <span @click="choose(2)"
            :class="{ active: choose_index == 2, up: sort.up == 1 && choose_index == 2, down: sort.down == 1 && choose_index == 2 }"
            class="grid-content">销量</span>
          <span @click="choose(3)"
            :class="{ active: choose_index == 3, up: sort.up == 1 && choose_index == 3, down: sort.down == 1 && choose_index == 3 }"
            class="grid-content">价格</span>
        </div>
      </div>
      <div class="list">
        <div class="list-item" v-for="item in list" :key="item.id" @click="link_goods(item)">
          <div class="img">
            <van-image width="100%" height="100%" :src="item.cover" />
          </div>
          <div class="title">
            <span>
              <span class="tik-tok-text">抖音</span>
              <span>{{ item.title }}</span>
            </span>
          </div>
          <div class="item-center">
            <div class="make-money" v-if="item.share_reward">
              <span class="p-4 fs-10 line">赚</span>
              <span class="p-4 fs-10">{{ item.share_reward }}</span>
            </div>
            <div class="buy">{{ item.sales }}</div>
          </div>
          <div class="item-bottom">
            <div class="c-FF6238" style="font-size: 0.75rem;">
              到手价
              <span class="ml-4" style="font-size: 0.875rem;">{{ $i18n.t('money') }}{{ item.coupon_price }}</span>
            </div>
            <div class="fs-12 c-999 line-through ml-12">{{ $i18n.t('money') }}{{ item.price }}</div>
          </div>
        </div>
      </div>
    </div>

    <van-overlay :show="show_dy_popup" @click="show_dy_popup = false">
      <div class="wrapper" @click.stop>
        <div class="block">
          <div class="dialog-title">{{activity_name}}</div>
          <div class="close" @click="show_dy_popup = false">
            <van-icon name="cross" />
          </div>
          <p class="link_dy">{{ dy_password }}</p>
          <div style="position: relative; bottom: 5%; left: 0; right: 0;" v-clipboard:copy="dy_password"
            v-clipboard:success="onCopy" v-clipboard:error="onError">
            <van-button style="width: 100%; border: none;" color="#EF3532">一键复制，打开抖音APP下单</van-button>
          </div>
        </div>
      </div>
    </van-overlay>

  </div>
</template>

<script>
import { Image as VanImage ,Toast} from "vant";
import { scrollMixin } from "../../../utils/mixin";
import cMent from "./component/menu.vue";
export default {
  mixins: [scrollMixin],
  components: { VanImage, cMent },
  data() {
    return {
      activity_name:"",
      dy_password:"",
      show_dy_popup:false,
      choose_index: 1,
      SearchValue: "",
      CarouseList: [],

      list: [],
      current_page: 1,
      last_page: 1,
      isLoadMore: true,

      order_type: 0,
      search_type: 0,
      cate_id: "",//一级分类id

      currrent_category_index: 0,
      sort: {
        up: 0,
        down: 0
      },
      category_list: [],
      button_group: [],

      show_flag:true,
    };
  },
  activated() {
    this.show_flag = true;
    this.isLoadMore = true;
    this.cate_id = '';
    console.log(this.$route);
    if(this.$route.query.album_id){
      this.show_flag  = false;
    }
    this.getCarouse();
    this.getCategory();
    this.buttonGroup();
    this.getData({ page: 1, search_type: this.search_type, order_type: this.order_type });
  },
  methods: {
    onCopy() {

        Toast({
          message: "复制成功",
          position: "bottom",
          duration: 1000
        });
      this.show_dy_popup = false;
    },
    onError() {
      Toast({
        message: "复制失败",
        position: "bottom",
        duration: 1000
      });
    },
    open_dialog(data){
      this.activity_name = data.name;
      $http.get("plugin.tiktok-cps.api.goods.aggregate-h5",{material_id:data.material_id}).then(res=>{
        if(res.result){
          console.log(res.data,"data=====");
          this.dy_password = res.data.command;
          if (this.fun.isCPS()) {
            let json = { dy_deeplink: res.data.deeplink };
            if (this.fun.isIosOrAndroid() === "ios") {
              IOStoTikTok(JSON.stringify(json))
            } else {
              androidToTikTok(res.data.deeplink)
            }
          }else{
            this.show_dy_popup = true;
          }
        }else{
          this.$toast(res.msg);
        }
      })
    },
    async buttonGroup() {
      let { data, msg, result } = await $http.get("plugin.tiktok-cps.api.goods.button-group");
      if (result == 0) return this.$toast(msg);
      this.button_group = data;
    },
    async getCategory() {
      let { data, msg, result } = await $http.get("plugin.tiktok-cps.api.goods.good-recommend-cate");
      if (result == 0) {
        return this.$toast(msg)
      }
      this.category_list = data || [];
    },
    async category_handle(id, i) {
      this.currrent_category_index = i;
      this.cate_id = id;
      this.getData({ page: 1, search_type: this.search_type, order_type: this.order_type, cate_id: id })
    },
    link_goods({ product_id }) {
      this.$router.push(this.fun.getUrl("TikTokDetail", { id: product_id }, {}));
    },
    link(url) {
      window.open(url, "_blank");
    },
    choose(index) {
      this.choose_index = index;
      let order_type = 0;
      if (index !== 1) {
        if (this.sort.up == 0 && this.sort.down == 0) {
          this.sort.up = 1;
        } else if (this.sort.up == 1) {
          this.sort.down = 1;
          this.sort.up = 0;
        } else if (this.sort.down == 1) {
          this.sort.up = 1;
          this.sort.down = 0;
        }
      } else if (this.choose_index == 1) {
        this.sort.up = 0;
        this.sort.down = 0;
      }
      if (this.sort.down == 1) {
        order_type = 1;
      }
      let search_type = index - 1;
      this.search_type = search_type;
      this.order_type = order_type;
      this.getData({ page: 1, search_type, order_type, cate_id: this.cate_id });
    },

    link() {
      this.$router.push(this.fun.getUrl("TikTokSearch"));
    },
    async getData(json) {
      let url = "plugin.tiktok-cps.api.goods.good-list";
      if(!this.show_flag){
        let {page }= json;
        json = {
          page,
          album_id:this.$route.query.album_id,
        }
        url = "plugin.tiktok-cps.api.goods.album-good-list";
      }
      let { result, msg, data } = await $http.get(url, json);
      if (result == 0) {
        return this.$toast(msg);
      }
      if (json.page == 1) {
        this.list = data.products || [];
      } else if (json.page > 1) {
        this.list = this.list.concat(data.products);
      }
      this.current_page = Number(data.current_page);
      this.last_page = data.last_page;
      this.isLoadMore = true;
    },
    async getCarouse() {
      let { result, msg, data } = await $http.get("plugin.tiktok-cps.api.rotation.rotation-list");
      if (result == 0) {
        return this.$toast(msg);
      }
      this.CarouseList = data || [];
    },
    getMoreData() {
      if (this.current_page >= this.last_page) {
        return;
      }
      this.isLoadMore = false;
      let page = this.current_page + 1;
      this.getData({ page, order_type: this.order_type, search_type: this.search_type, cate_id: this.cate_id });
    }
  }
};
</script>

<style lang='scss'>
#tiktok-cps {

.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}
.dialog-title{
  text-align: center;
  font-size: 1.5rem;
}
.link_dy {
  margin-top: 1.0625rem;
  margin-bottom: 1.25rem;
  overflow: hidden;
  word-break: break-all;
  text-align: left;
  color: #999;
  font-size: 0.75rem;
}
.close {
  position: absolute;
  right: 5%;
  top: 5%;
}

.block {
  position: relative;
  width: 17.5rem;
  border-radius: 0.25rem;
  background-color: #fff;
  padding: 1.25rem 0.625rem;
}

  .ment {
    background: #fff;
    margin-bottom: 0.5rem;
  }

  .current_category {
    color: var(--themeBaseColor);
  }

  .category {
    display: flex;
    padding: 0.75rem;
    overflow-x: auto;

    span {
      margin-right: 1.5625rem;
      font-size: 0.8125rem;
      flex-shrink: 0;
    }
  }

  .category::-webkit-scrollbar {
    display: none;
  }

  .list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-evenly;
    background: #f1f1f1;
    padding-bottom: 3.125rem;
  }

  .grid-content::after {
    content: "";
    display: inline-block;
    width: 0.5rem;
    height: 0.625rem;
    background: url(../../../assets/images/jd-sprites.png) no-repeat;
    background-size: 12.5rem 12.5rem;
    background-position: -10.625rem -1.875rem;
    margin-left: 0.25rem;
  }

  .grid-content.up,
  .grid-content.down {
    color: #f15353;
  }

  .grid-content.up::after {
    background-position: -10.625rem -0.9375rem;
  }

  .grid-content.down::after {
    background-position: -10.625rem 0;
  }

  .p-4 {
    padding: 0.1rem 0.25rem;
  }

  .item-center {
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding: 0 0.5rem;
  }

  .img {
    width: 100%;
    height: 10.5938rem;
  }

  .flex {
    display: flex;
  }

  .fs-10 {
    font-size: 0.625rem;
  }

  .between {
    justify-content: space-between;
  }

  .sort {
    position: relative;
  }

  .sort-item {
    position: absolute;
    right: -20px;
    top: 0;
    bottom: 0;
    width: 20px;

    &::after {
      content: "";
      left: 0;
      top: 0;
      position: absolute;
      border: 5px solid #000;
      border-top-color: transparent;
      border-bottom-color: #ccc;
      border-left-color: transparent;
      border-right-color: transparent;
    }
  }

  .item-bottom {
    width: 100%;
    display: flex;
    align-items: center;
    padding: 0.25rem 0.5rem 0.9rem;
  }

  .ml-4 {
    margin-left: 0.25rem;
  }

  .line-through {
    text-decoration: line-through;
  }

  .ml-12 {
    margin-left: 0.75rem;
  }

  .c-999 {
    color: #999;
  }

  .c-FF6238 {
    color: #ff6238;
  }

  .fs-12 {
    font-size: 0.75rem;
  }

  .make-money {
    border-radius: 0.25rem;
    display: flex;
    justify-content: space-between;
    color: #ff7b05;
    border: 1px solid #ff7b05;

    .line {
      position: relative;

      &::after {
        content: "";
        position: absolute;
        top: 0;
        bottom: 0;
        right: 0;
        width: 0.0313rem;
        height: 100%;
        background: #ff7b05;
      }
    }
  }

  .list-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 46%;
    overflow: hidden;
    background: #fff;
    border-radius: 0.5rem;
    overflow: hidden;
    margin: 0.75rem 0.375rem 0;
  }

  .tik-tok-text {
    color: rgb(255, 255, 255);
    background: rgb(0, 0, 0);
    padding: 0 5px;
    border-radius: 5px;
    margin-right: 5px;
    font-size: 0.75rem;
  }

  .title {
    display: flex;
    text-align: left;
    padding: 0.1875rem 0.5rem 0.375rem;
  }

  .buy {
    color: #999;
    font-size: 0.75rem;
  }

  .active {
    color: #ef5452;
  }

  .content {
    background: #fff;
  }

  .tab {
    display: flex;
    justify-content: space-evenly;
    padding-top: 10px;
    padding-bottom: 12px;
  }

  .van-search {
    flex: 1;
  }

  .top-right {
    display: flex;
    align-items: center;
    flex-direction: column;
    margin: 10px 15px;
    padding-top: 10px;
  }

  .top {
    display: flex;
    background: #fff;
    align-items: center;
  }
}
</style>